import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import store from "@/store/index"

const routes = [
	{
		path: '/login',
		name: 'Login',
		// route level code-splitting
		// this generates a separate chunk (about.[hash].js) for this route
		// which is lazy-loaded when the route is visited.
		component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
	},
	{
		path: '/',
		name: 'Home',
		component: Home
	},
	// 管理员
	{
		path:"/admin",
		name:"AdminIndex",
		// 重定向
		redirect:"/admin/userManage",
		component:()=>import("../views/admin/AdminIndex"),
		// 子路由
		children:[
			{
				path:"userManage",
				component:()=>import("../views/admin/UserManage")
			},
		]
	},
	// 教务管理员
	{
		path:"/educationalAdmin",
		name:"EducationalAdminIndex",
		// 重定向
		redirect: "/educationalAdmin/userManage",
		component:()=>import("../views/educationalAdmin/EducationalAdminIndex"),
		// 子路由
		children:[
			{
				path: "userManage",
				component:()=>import("../views/educationalAdmin/UserManage")
			},
			{
				path:"courseManage",
				component:()=>import("../views/educationalAdmin/CourseManage")
			},
		]
	},
	// 教师
	{
		path:"/teacher",
		name:"TeacherIndex",
		// 重定向
		redirect: "/teacher/index",
		component:()=>import("../views/teacher/TeacherIndex"),
		// 子路由
		children:[
			{
				path: "index",
				component:()=>import("../views/teacher/CourseListIndex")
			},
			{
				path:"courseInfo",
				component:()=>import("../views/teacher/CourseInfoIndex"),
				redirect: "/teacher/courseInfo/courseBaseInfo",
				// 三级子路由
				children:[
					{
						path:"courseBaseInfo",
						component:()=>import("../views/teacher/courseInfo/CourseBaseInfo")
					},
					{
						path:"notice",
						component:()=>import("../views/teacher/courseInfo/Notice")
					},
					{
						path:"homework",
						component:()=>import("../views/teacher/courseInfo/Homework")
					},
					{
						path:"resource",
						component:()=>import("../views/teacher/courseInfo/Resource")
					},
					{
						path:"chapter",
						component:()=>import("../views/teacher/courseInfo/Chapter")
					},
					{
						path:"recordData",
						component:()=>import("../views/teacher/courseInfo/RecordData")
					},
				]
			}
		]
	},
	{
		path:"/student",
		name:"StudentIndex",
		// 重定向
		redirect: "/student/index",
		component:()=>import("../views/student/StudentIndex"),
		// 子路由
		children:[
			{
				path:"index",
				component:()=>import("../views/student/StudentCourseListIndex")
			},
			{
				path:"courseInfo",
				component:()=>import("../views/student/StudentCourseInfoIndex"),
				redirect: "/student/courseInfo/index",
				// 三级子路由
				children:[
					{
						path:"index",
						component:()=>import("../views/student/courseInfo/StudentICourseInfoChapter")
					},
					{
						path:"resource",
						component:()=>import("../views/teacher/courseInfo/Resource")
					},
					{
						path:"homework",
						component:()=>import("../views/student/courseInfo/StudentHomework")
					},
					{
						path:"homeworkEdit",
						component:()=>import("../views/student/courseInfo/HomeworkEdit")
					},
				]
			},
		]
	},
	{
		path:"/video",
		name:"Video",
		component:()=>import("../views/student/Video"),
	},
	// 大文件上传测试页面
	{
		path:"/upload",
		name:"UpLoadTest",
		component:()=>import("../temp/UpLoadTest"),
	},

]


const router = createRouter({
	history: createWebHashHistory(),
	routes
})

// 定义完路由后，我们主要是利用vue-router提供的钩子函数beforeEach()对路由进行判断。
router.beforeEach((to, from, next) => {
	if (to.name!="Login") {  // 判断该路由是否需要登录权限
		if (store.state.token&&store.state.token!="") {  // 通过vuex state获取当前的token是否存在
			next();
		}
		else {
			console.log("router/index.js40：未登录");
			window.localStorage.removeItem('loged');
			window.localStorage.removeItem('store');
			next({
				path: '/login',
				query: {redirect:router.currentRoute.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
			})
		}
	}
	else {
		next();
	}
})


export default router
